<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//


require_once($WSC->get_site_inc_dir().'modules/strawberry_ajax_form_ctrl/strawberry_ajax_form_ctrl.class.php');


abstract class Chocolate_Ajax_Form_Ctrl extends Strawberry_Ajax_Form_Ctrl {
	private $dbc;
	private $dbc_pnt;

	/**
	 * Creates new DB connection.
	 *
	 * Please note that if persistence is used there is no guarantee that returned connection will be newly created.
	 *
	 * @return DB_Connection
	 */
	final public function create_dbc() {
		global $_SITE_DBC;

		if (isset($_SITE_DBC)) {
			if ($_SITE_DBC instanceof DB_Connection) {
				if ($_SITE_DBC->is_connected()) {
					$this->dbc = $_SITE_DBC;
				}
			}
		}

		if (!($this->dbc instanceof DB_Connection)) {
			$db_config = $this->_config->get_db_config();
			$this->dbc = new Site_DBC($db_config);
			$_SITE_DBC = $this->dbc;
		}


		return $this->dbc;
	}


	/**
	 * Returns connected DB_Connection.
	 *
	 * @return DB_Connection
	 */
	public function get_dbc() {
		if ($this->dbc instanceof DB_Connection) {
			if (!$this->dbc->is_connected()) {
				$this->dbc = $this->create_dbc();
			}
		} else {
			$this->dbc = $this->create_dbc();
		}

		$ret = $this->dbc;

		return $ret;
	}


	/**
	 * Creates new persistent DB connection withOUT transactions
	 *
	 * @return DB_Connection
	 */
	final public function create_dbc_pnt() {
		global $_SITE_DBC_PNT;

		if (isset($_SITE_DBC_PNT)) {
			if ($_SITE_DBC_PNT instanceof DB_Connection) {
				if ($_SITE_DBC_PNT->is_connected()) {
					$this->dbc_pnt = $_SITE_DBC_PNT;
				}
			}
		}

		if (!($this->dbc_pnt instanceof DB_Connection)) {
			$db_config = $this->_config->get_db_config();
			$this->dbc_pnt = new Site_dbc_pnt($db_config);
			$_SITE_DBC_PNT = $this->dbc_pnt;
		}


		return $this->dbc_pnt;
	}


	/**
	 * Returns connected DB_Connection.
	 *
	 * @return DB_Connection
	 */
	public function get_dbc_pnt() {
		if ($this->dbc_pnt instanceof DB_Connection) {
			if (!$this->dbc_pnt->is_connected()) {
				$this->dbc_pnt = $this->create_dbc_pnt();
			}
		} else {
			$this->dbc_pnt = $this->create_dbc_pnt();
		}

		$ret = $this->dbc_pnt;

		return $ret;
	}
}
